<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>es6封装函数测试</title>
</head>
<body>
    <script>
    //获取字符串中每个字符出现的个数,并指出出现次数最多的那个字符
    class Statistics{
        constructor(arr){
            this.arr = arr;
            this.arrGroup = {};
            this.arrMax = 0;
            this.arrKey = '';
            console.log('您输入的内容是'+this.arr);
        }
        calculate(){
            let _this = this;
            //生成统计组
            for(var i=0;i<_this.arr.length;i++){
                if(_this.arrGroup.hasOwnProperty(_this.arr[i])){
                    _this.arrGroup[_this.arr[i]] = _this.arrGroup[_this.arr[i]] + 1;
                }else{
                    _this.arrGroup[_this.arr[i]] = 1;
                }
            }

            console.log(_this.arrGroup);
            
            //循环统计组，找出出现最多次数的字符以及次数
            for(var key in _this.arrGroup){
                if(_this.arrGroup[key] > _this.arrMax){
                    _this.arrMax = _this.arrGroup[key];
                    _this.arrKey = key;
                }
            }

            console.log('出现最多的是：'+ _this.arrKey+',出现了：'+ _this.arrMax+'次');
            
        }
    }

 const statistics = new Statistics('Starbucks witnessed an operating profit decline in the third quarter of this year, markingthe first decline in the past nine years.');
 statistics.calculate();
    </script>
</body>
</html>